package com.miaoji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miaoji.domain.po.Cloth;
import com.miaoji.domain.po.Combo;
import com.miaoji.domain.po.ComboDetail;
import com.miaoji.domain.vo.ComboVo;
import com.miaoji.mapper.ComboDetailMapper;
import com.miaoji.mapper.ComboMapper;
import com.miaoji.service.ComboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ComboServiceImpl implements ComboService {

    @Autowired
    ComboMapper comboMapper;
    @Autowired
    ComboDetailMapper comboDetailMapper;

    @Override
    public void ComboNew(Combo combo) {
        comboMapper.insert(combo);
    }

    @Override
    public void ComboUpdate(Combo combo) {
        comboMapper.updateById(combo);
    }

    @Override
    public void ComboDelete(Combo combo) {
        comboMapper.deleteById(combo.getId());
        comboDetailMapper.comboDetailDelete(combo.getId());
    }



    @Override
    public List<Combo> ComboSelect(Combo combo) {
        QueryWrapper<Combo> comboQueryWrapper = new QueryWrapper<>();
        if (combo.getComboName()!=null && !combo.getComboName().isEmpty()){
            comboQueryWrapper.like("combo_name", combo.getComboName());
        }
        if (combo.getComment()!=null && !combo.getComment().isEmpty()){
            comboQueryWrapper.like("comment", combo.getComment());
        }
        if (combo.getId()!=null){
            comboQueryWrapper.eq("id", combo.getId());
        }
        if (combo.getState()!=null){
            comboQueryWrapper.eq("state", combo.getState());
        }
        return comboMapper.selectList(comboQueryWrapper);
    }

    @Override
    public List<Combo> ComboSelectAll() {
        return comboMapper.select();
    }

    @Override
    public void ComboDetaileNew(ComboDetail comboDetail) {
        comboDetailMapper.insert(comboDetail);
    }

    @Override
    public void ComboDetailDelete(ComboDetail comboDetail) {
        comboDetailMapper.deleteById(comboDetail.getId());
    }

    @Override
    public void ComboDetailUpdate(ComboDetail comboDetail) {
        comboDetailMapper.updateById(comboDetail);
    }

    @Override
    public List<ComboDetail> ComboDetailSelect(Combo combo) {
        return comboDetailMapper.comboDetailSelect(combo.getId());
    }

    @Override
    public List<ComboVo> ComboselectAllList() {
        List<Combo> combos = comboMapper.select();
        List<ComboVo> comboVos = new ArrayList<>();
        for (Combo i:combos) {
            ComboVo comboVo = new ComboVo();
            List<ComboDetail> comboDetails = comboDetailMapper.comboDetailSelect(i.getId());
            comboVo.setCombo(i);
            comboVo.setComboDetails(comboDetails);
            comboVos.add(comboVo);
        }
        return comboVos;
    }


}
